home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 11502 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.1 KB  |  71 lines

  1. Path: nntp-server.caltech.edu!news
  2. From: Xuhua Li <xuhua@cco.caltech.edu>
  3. Newsgroups: comp.lang.c++
  4. Subject: C++ newbie asks for help
  5. Date: Thu, 14 Mar 1996 13:08:24 -0800
  6. Organization: California Institute of Technology, Pasadena
  7. Message-ID: <31488AC8.1557@cco.caltech.edu>
  8. NNTP-Posting-Host: xuhua-ppp.caltech.edu
  9. Mime-Version: 1.0
  10. Content-Type: text/plain; charset=gb2312
  11. Content-Transfer-Encoding: 7bit
  12. X-Mailer: Mozilla 2.0GoldB1 (Win95; I)
  13.  
  14. The following C++ programs from Stephen Prata's book <<C++ Primer Plus>>
  15. does not work properly compiled by Microsoft C++ 4.0 under Window 95. 
  16. Can anybody help me out there?
  17. Program #1:
  18. // waiting.cpp -- using clock() in a time-delay loop
  19. #include <iostream.h> 
  20. #include <time.h> // describes clock() function, clock_t type 
  21. int main(void) 
  22. {
  23.     cout << "Enter the delay time, in seconds: "; 
  24.     float secs; cin >> secs;
  25.         clock_t delay = secs * CLOCKS_PER_SEC;  
  26.                                   // convert to clock ticks 
  27.     cout << "starting\a\n";
  28.         clock_t start = clock();
  29.         while (clock() - start < delay )       
  30.                                      // wait until time elapses
  31.                 ; 
  32.     cout << "done \a\n"; 
  33.     return 0;
  34. }
  35. After I compile the program using Visual C++ 4.0's command line compiler
  36. cl, I run the .exe file waiting.exe. It is supposed to work in the 
  37. following way:
  38. Enter the delay time: (I input 10 here)
  39. starting\a
  40. (The computer waits for 10 sec.)
  41. done\a.
  42. But it works in the following way indeed:
  43. Enter the delay time: (I input 10)
  44. (The conputer wait for 10 secs)
  45. starting\a
  46. done\a.
  47. I do not know what happened!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  48.  
  49. Program #2
  50. // textin3.cpp -- reading chars to end of file
  51. #include <iostream.h>
  52. int main(void)
  53. {
  54.     char ch;
  55.     int count = 0;
  56.  
  57.     while (cin.get(ch))      // cin.get(ch) is 0 on EOF
  58.     {
  59.         cout << ch;
  60.         count++;
  61.     }
  62.     cout << count << " characters read\n";
  63.     return 0;
  64. }
  65. After I input the simulated EOF CTRL-Z, the problem quits without 
  66. reporting the count. 
  67.  
  68. They are very simple program but work strange, can you tell me what 
  69. is wrong with them.
  70. Your assistance will be highly appriciated.
  71.